<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ishowshao
 * Date: 11-10-26
 */

include_once '../common.func.php';

$error = '';

$list = array('url', 'title', 'category', 'tags', 'content', 'action');
if (!checkIsset($_POST, $list)) {
    exit('isset');
}

$url = trim($_POST['url']);

$old = getPostById($url);

$tags = array();
if (trim($_POST['tags']) !== '') {
    $tags = explode(',', $_POST['tags']);
    foreach ($tags as $key => $value) {
        $tags[$key] = trim($value);
    }
}

$post = array(
    'url' => $url,
    'title' => trim($_POST['title']),
    'content' => trim($_POST['content']),
    'category' => intval($_POST['category']),
    'time' => $old['time'],
    'tag' => $tags,
    'comments' => $old['comments'],
);
if (!updatePostById($url, $post)) {
    $error = 'updatePostByIdFail';
}
//addPostToCategory($url, $post['category']);
//updateTags($url, $tags);
compilePostById($url, $post);

header('Location: ' . SITE_URL . '/admin/posts.php?error=' . $error);
